/**
 * 
 */
package com.lanswon.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**   
* 
* @ClassName: ImageUtil.java
* @Description: 图片功能工具类
*
* @version: v1.0.0
* @author: zm
* @date: 2018年5月2日 下午1:53:20 
*
*/
public class ImageUtil {
	 

	 
	     public static byte[] imageToByte(MultipartFile multfile) throws IOException {
	    	 
	    	 CommonsMultipartFile cf = (CommonsMultipartFile)multfile;  
	    	 DiskFileItem fi = (DiskFileItem) cf.getFileItem(); 
	    	 File file = fi.getStoreLocation();
	    	 
	    	 if(!file.exists()){
	    	     //先得到文件的上级目录，并创建上级目录，在创建文件
	    	     file.getParentFile().mkdir();
	    	     try {
	    	         //创建文件
	    	         file.createNewFile();
	    	     } catch (IOException e) {
	    	         e.printStackTrace();
	    	     }
	    	 }
	    	 
	         FileInputStream fileInputStream = new FileInputStream(file);
	         byte[] bytes = new byte[fileInputStream.available()];
	         fileInputStream.read(bytes);
	         fileInputStream.close();

	         return bytes;
	     }
	     
	}
